﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace produccion.clases
{
    public class CLSMAEREFP:ObservableObject
    {
        
        MDOgeneral mod = new MDOgeneral();

        #region Metodos basicos tabla

        public DataTable Consultar()
        {
            try
            {
                string query = "select material_ref, forro_ref, suela_ref from maerefp where codref_ref='" + CODREF_REF + "'";
                DataTable dt = mod.consulta(query);
                if (dt.Rows.Count > 0)
                {
                    MATERIAL_REF = dt.Rows[0][0].ToString();
                    FORRO_REF = dt.Rows[0][1].ToString();
                    SUELA_REF = dt.Rows[0][2].ToString();
                }
                else
                {
                    MATERIAL_REF = "";
                    FORRO_REF = "";
                    SUELA_REF = "";
                }
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public void consultar()
        {
            try
            {
                string query = "select CODREF_REF,MATERIAL_REF,FORRO_REF,SUELA_REF,LINPRE_REF,LINCOR_REF,LINGUA_REF,LINMON_REF,HORMA_REF,TRQTROMPA_REF,TRQCONTRA_REF,TRQFORROPLAN_REF, CODLIN_REF from MAEREFP where (CODREF_REF='" + CODREF_REF + "')";
                DataTable val = mod.consulta(query);
                if (val.Rows.Count > 0)
                {
                    this.CODREF_REF = val.Rows[0]["CODREF_REF"].ToString();
                    this.MATERIAL_REF = val.Rows[0]["MATERIAL_REF"].ToString();
                    this.FORRO_REF = val.Rows[0]["FORRO_REF"].ToString();
                    this.SUELA_REF = val.Rows[0]["SUELA_REF"].ToString();
                    this.LINPRE_REF = val.Rows[0]["LINPRE_REF"].ToString();
                    this.LINCOR_REF = val.Rows[0]["LINCOR_REF"].ToString();
                    this.LINGUA_REF = val.Rows[0]["LINGUA_REF"].ToString();
                    this.LINMON_REF = val.Rows[0]["LINMON_REF"].ToString();
                    this.HORMA_REF = val.Rows[0]["HORMA_REF"].ToString();
                    this.TRQTROMPA_REF = val.Rows[0]["TRQTROMPA_REF"].ToString();
                    this.TRQCONTRA_REF = val.Rows[0]["TRQCONTRA_REF"].ToString();
                    this.TRQFORROPLAN_REF = val.Rows[0]["TRQFORROPLAN_REF"].ToString();

                    this.CODLIN_REF = val.Rows[0]["CODLIN_REF"].ToString();//FJRF - Asigamos el valor retornado
                    if (String.IsNullOrEmpty(CODLIN_REF) == true)//FJRF - Si el valor es nulo o vacio hacemos lo siguiente
                    {
                        this.CODLIN_REF = "ZZZ999";//FJRF - Asignamos un codigo por defecto para que cambie el combo
                    }

                    EXISTE = true;
                    CAMBIO = false;
                }
                else
                {
                    this.CODREF_REF = "";
                    this.MATERIAL_REF ="";
                    this.FORRO_REF = "";
                    this.SUELA_REF = "";
                    this.LINPRE_REF = "";
                    this.LINCOR_REF = "";
                    this.LINGUA_REF = "";
                    this.LINMON_REF = "";
                    this.HORMA_REF = "";
                    this.TRQTROMPA_REF = "";
                    this.TRQCONTRA_REF = "";
                    this.TRQFORROPLAN_REF = "";
                    this.CODLIN_REF = "ZZZ999";//FJRF - Asignamos un codigo por defecto para que cambie el combo
                    CAMBIO = false;
                    EXISTE = false;
                }

            }
            catch (Exception e) { throw e; }
        }

        public int eliminar()
        {
            try{
            string query = "delete from MAEREFP where (CODREF_REF='" + CODREF_REF + "')";
            return mod.comando(query);
            }catch (Exception e) { throw e; }
        }

        public int actualizar()
        {
            try{
                string query = "update MAEREFP set MATERIAL_REF='" + MATERIAL_REF + "',FORRO_REF='" + FORRO_REF + "',SUELA_REF='" + SUELA_REF + "',LINPRE_REF='" + LINPRE_REF + "',LINCOR_REF='" + LINCOR_REF + "',LINGUA_REF='" + LINGUA_REF + "',LINMON_REF='" + LINMON_REF + "',HORMA_REF='" + HORMA_REF + "',TRQTROMPA_REF='" + TRQTROMPA_REF + "',TRQCONTRA_REF='" + TRQCONTRA_REF + "',TRQFORROPLAN_REF='" + TRQFORROPLAN_REF + "', CODLIN_REF='" + CODLIN_REF + "' where (CODREF_REF='" + CODREF_REF + "')";
            return mod.comando(query);
            }catch (Exception e) { throw e; }
        }

        public int insertar()
        {
            try{
                string query = "insert into MAEREFP (CODREF_REF,MATERIAL_REF,FORRO_REF,SUELA_REF,LINPRE_REF,LINCOR_REF,LINGUA_REF,LINMON_REF,HORMA_REF,TRQTROMPA_REF,TRQCONTRA_REF,TRQFORROPLAN_REF, CODLIN_REF) values ('" + CODREF_REF + "','" + MATERIAL_REF + "','" + FORRO_REF + "','" + SUELA_REF + "','" + LINPRE_REF + "','" + LINCOR_REF + "','" + LINGUA_REF + "','" + LINMON_REF + "','" + HORMA_REF + "','" + TRQTROMPA_REF + "','" + TRQCONTRA_REF + "','" + TRQFORROPLAN_REF + "', '" + CODLIN_REF + "')";
            return mod.comando(query);
            }catch (Exception e) { throw e; }
        }

        public int InsertarRefCole(DataTable coleccion, DataTable colores)
        {
            MDOgeneral.transaccion = MDOgeneral.conexion.BeginTransaction();
            try
            {
                guardarColeccion(coleccion, colores);
                int i = insertar();
                MDOgeneral.transaccion.Commit();

                return i;
            }
            catch (Exception ex)
            {
                MDOgeneral.transaccion.Rollback();
                throw ex;
            }
        }

        public int ActualizarRefCole(DataTable coleccion, DataTable colores)
        {
         
            MDOgeneral.transaccion = MDOgeneral.conexion.BeginTransaction();
            try
            {
                guardarColeccion(coleccion, colores);
                int i = actualizar();
                MDOgeneral.transaccion.Commit();

                return i;
            }
            catch (Exception ex)
            {
                MDOgeneral.transaccion.Rollback();
                throw ex;
            }

        }

        private void guardarColeccion(DataTable coleccion,DataTable colores)
        {
            try{
            clases.CLSREFCLCP REFCLCP = new CLSREFCLCP();

             foreach (DataRow r in coleccion.Select(null, null, DataViewRowState.ModifiedCurrent))
            {
                foreach (DataRow rc in colores.Rows)
                {
                    REFCLCP.CODREF_RXC = CODREF_REF;
                    REFCLCP.CODCOL_RXC = rc[0].ToString();
                    REFCLCP.CODCLC_RXC = r[0].ToString();

                    if (r[2].ToString() == "True")
                    {
                        if (!REFCLCP.ExisteRefCole())
                        {
                            REFCLCP.insertarRefCole();
                        }
                    }
                    else
                    {
                        REFCLCP.EliminarRefCol();
                    }
                }
            }
           }catch (Exception e) { throw e; }

        }

        public Boolean existe()
        {
            try{
            string query = "select  coalesce(count(CODREF_REF),0) from MAEREFP where (CODREF_REF='" + CODREF_REF + "')";
            DataTable val = mod.consulta(query);
            if (val.Rows[0][0].ToString().Equals("0"))
            {
                return false;
            }
            return true;
            }catch (Exception e) { throw e; }
        }

        #endregion

        #region  Propiedades tabla MAEREFP

        private string _CODLIN_REF;
        public string CODLIN_REF
        {
            get { return _CODLIN_REF; }
            set { _CODLIN_REF = value; RaisePropertyChangedEvent("CODLIN_REF"); }
        }

        private string _CODREF_REF;
        public string CODREF_REF
        {
            get { return _CODREF_REF; }
            set { _CODREF_REF = value; RaisePropertyChangedEvent("CODREF_REF"); }
        }
        private string _MATERIAL_REF;
        public string MATERIAL_REF
        {
            get { return _MATERIAL_REF; }
            set
            {
                if (_MATERIAL_REF != value) { CAMBIO = true; } _MATERIAL_REF = value;
                RaisePropertyChangedEvent("MATERIAL_REF");
            }
        }
        private string _FORRO_REF;
        public string FORRO_REF
        {
            get { return _FORRO_REF; }
            set
            {
                if (_FORRO_REF != value) { CAMBIO = true; } _FORRO_REF = value;
                RaisePropertyChangedEvent("FORRO_REF");
            }
        }
        private string _SUELA_REF;
        public string SUELA_REF
        {
            get { return _SUELA_REF; }
            set
            {
                if (_SUELA_REF != value) { CAMBIO = true; } _SUELA_REF = value;
                RaisePropertyChangedEvent("SUELA_REF");
            }
        }
        private string _LINPRE_REF;
        public string LINPRE_REF
        {
            get { return _LINPRE_REF; }
            set
            {
                if (_LINPRE_REF != value) { CAMBIO = true; } _LINPRE_REF = value;
                RaisePropertyChangedEvent("LINPRE_REF");
            }
        }
        private string _LINCOR_REF;
        public string LINCOR_REF
        {
            get { return _LINCOR_REF; }
            set
            {
                if (_LINCOR_REF != value) { CAMBIO = true; } _LINCOR_REF = value;
                RaisePropertyChangedEvent("LINCOR_REF");
            }
        }
        private string _LINGUA_REF;
        public string LINGUA_REF
        {
            get { return _LINGUA_REF; }
            set
            {
                if (_LINGUA_REF != value) { CAMBIO = true; } _LINGUA_REF = value;
                RaisePropertyChangedEvent("LINGUA_REF");
            }
        }
        private string _LINMON_REF;
        public string LINMON_REF
        {
            get { return _LINMON_REF; }
            set
            {
                if (_LINMON_REF != value) { CAMBIO = true; } _LINMON_REF = value;
                RaisePropertyChangedEvent("LINMON_REF");
            }
        }
        private string _HORMA_REF;
        public string HORMA_REF
        {
            get { return _HORMA_REF; }
            set
            {
                if (_HORMA_REF != value) { CAMBIO = true; } _HORMA_REF = value;
                RaisePropertyChangedEvent("HORMA_REF");
            }
        }
        private string _TRQTROMPA_REF;
        public string TRQTROMPA_REF
        {
            get { return _TRQTROMPA_REF; }
            set
            {
                if (_TRQTROMPA_REF != value) { CAMBIO = true; } _TRQTROMPA_REF = value;
                RaisePropertyChangedEvent("TRQTROMPA_REF");
            }
        }
        private string _TRQCONTRA_REF;
        public string TRQCONTRA_REF
        {
            get { return _TRQCONTRA_REF; }
            set
            {
                if (_TRQCONTRA_REF != value) { CAMBIO = true; } _TRQCONTRA_REF = value;
                RaisePropertyChangedEvent("TRQCONTRA_REF");
            }
        }
        private string _TRQFORROPLAN_REF;
        public string TRQFORROPLAN_REF
        {
            get { return _TRQFORROPLAN_REF; }
            set
            {
                if (_TRQFORROPLAN_REF != value) { CAMBIO = true; } _TRQFORROPLAN_REF = value;
                RaisePropertyChangedEvent("TRQFORROPLAN_REF");
            }
        }
        private Boolean _CAMBIO = false;
        public Boolean CAMBIO
        {
            get { return _CAMBIO; }
            set { _CAMBIO = value; RaisePropertyChangedEvent("CAMBIO"); }
        }
        private Boolean _EXISTE = false;
        public Boolean EXISTE
        {
            get { return _EXISTE; }
            set { _EXISTE = value; RaisePropertyChangedEvent("EXISTE"); }
        }
        #endregion
    }
}
